#include "drive.h"

void pwm0_on(int hz) // hz:频率
{

    int fd = open("/dev/pwm0", O_RDWR);
    // int i = 1000000 / hz;
    int n = hz;
    while (n--)
    {
        ioctl(fd, PWMON);
        // usleep(i);

        // ioctl(fd, PWMOFF);
        // usleep(i);
    }

    close(fd);
}

void led_on(int n)
{

    int fd = open("/dev/led3", O_RDWR);
    while (n--)
    {
        ioctl(fd, LEDON);
        sleep(1);
    }

    close(fd);
}

void key()
{

    int fd = open("/dev/key3", O_RDWR);

    while (1)
    {
        char c;
        read(fd, &c, 1);
        printf("%c\n", c);
    }

    close(fd);
}

#if 0
void ds18b20(int *arg)
{
    int fd;
    int connfd = *(int *)arg; // connfd:套接字
    short temp;
    char zheng, fen;
    float temputer, resolution;
    char buf[64] = {0}; //定义一个数组用于后面传输
    fd = open(PATH, O_RDWR);
    if (fd < 0)
    {
        perror("open");
        exit(-EINVAL);
    }
    if (ioctl(fd, temp_12, &resolution))
    {
        perror("ioctl \n");
        exit(-EINVAL);
    }
    while (1)
    {
        if (!read(fd, &temp, sizeof(short)))
        {
            perror("read");
        }
        zheng = temp >> 4;
        fen = temp & 0xf;
        if (zheng & (1 << 8))
        {
            temputer = (temp - 65535) * resolution;
        }
        else
        {
            temputer = zheng + fen * resolution;
        }
        sleep(1);
        if ((temputer >= (-55)) && (temputer <= 125))
            // printf("temp :%d \t resouttion :%0.3f ,tempter:%0.3f\n",temp ,resolution,temputer);
            sprintf(buf, "temp :%d \t resouttion :%0.3f ,tempter:%0.3f\n", temp, resolution, temputer);
        int ret = write(connfd, buf, sizeof(buf));
        if (ret < 0)
        {
            break;
        }
        memset(buf, 0, sizeof(buf));
    }
    close(fd);
}
#endif
